# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=ag
_longname=the_silver_searcher
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.0
pkgrel=2
pkgdesc="The Silver Searcher: An attempt to make something better than ack, which itself is better than grep (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://geoff.greer.fm/ag"
license=("Apache")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs') # '!strip' 'debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/ggreer/the_silver_searcher/archive/${pkgver}.tar.gz"
        "0001-lang-Allow-both-extensions-and-full-filenames.patch"
        "0002-win32-Detect-Cygwin-MSYS-PTY.patch"
        "0003-options-Fix-ordering-problems-with-color.patch"
        "0004-lang-Add-autotools-ac-am-in-m4-pc.patch"
        "0005-lang-Add-to-make-Makefile-Makefile.Debug-Makefile.Re.patch"
        "0006-lang-Add-makepkg-PKGBUILD-diff-patch-in-install.patch"
        "0007-lang-Add-cmake-CMakeLists.txt-CMakeCache.txt.patch"
        "0008-Fix-multiple-global-symbols-definitions.patch"
        "0009-win32-Use-NUL-for-git-config.patch")
sha256sums=('6a0a19ca5e73b2bef9481c29a508d2413ca1a0a9a5a6b1bd9bbd695a7626cbf9'
            'dd33d804a654073acbb78cde1caf7b7c6feea13965f1cfa68588f84f4adcc8ef'
            '247a0008a00cbd6c045c6872d61ded1fa455e31faff74ef6932fd4f921681f0d'
            'da69609723c4f019f822af81163914f924998575c009836cd2d4ea887acd4977'
            '1972392e4762bff91d4e542d478c0a01ddd4dbd507543867aa3b2dd3f159084b'
            'a16340cc33cdf95300f353ae87d3af2a0c667987ea9ce9b8c77d3e5c9a8a1007'
            'f38d5807ba0486e5004ff86d76a222221a390ac58475c464bc446d4154da8d75'
            '88fc3742a0bea4a1f1d533ad4f0b841423930e40639d4a9ea16312eadf21415a'
            'ae8cc32f94432b6482a379b0fdfc4095c836f3a947363a101ca69362b5a7f0a1'
            '6d98aedccb3c380fe2069373e8b45ddcc8d6c4936cb37da7967a7d5d29557164')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/${_longname}-${pkgver}
  apply_patch_with_msg \
    0001-lang-Allow-both-extensions-and-full-filenames.patch \
    0002-win32-Detect-Cygwin-MSYS-PTY.patch \
    0003-options-Fix-ordering-problems-with-color.patch \
    0004-lang-Add-autotools-ac-am-in-m4-pc.patch \
    0005-lang-Add-to-make-Makefile-Makefile.Debug-Makefile.Re.patch \
    0006-lang-Add-makepkg-PKGBUILD-diff-patch-in-install.patch \
    0007-lang-Add-cmake-CMakeLists.txt-CMakeCache.txt.patch \
    0008-Fix-multiple-global-symbols-definitions.patch \
    0009-win32-Use-NUL-for-git-config.patch

  # configure.ac forces -O2, so force it to -O0 if debugging.
  if check_option "debug" "y"; then
    sed -i "s#-O2#-O0#g" configure.ac
  fi

  autoreconf -fiv
}

build() {

  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  "${srcdir}"/${_longname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --{build,host}=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}"/${_longname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
